package com.tju.eduservice.TestSocket;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class client1 {
    public static void main(String[] args) {

    }
    static Socket client;
    static {
        try {
            client = new Socket("localhost", 6666);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void client1() throws IOException, InterruptedException {

        OutputStream os = client.getOutputStream();

        InputStream inputStream = client.getInputStream();
        while (true){
            Thread.sleep(2000);
            System.out.println("client1发送之前");
            os.write("00000000000000000".getBytes());
            byte[] bytes = new byte[1024];
            int read = inputStream.read(bytes);
            if(read != -1){
                String resp = new String(bytes, 0, read);
                System.out.println(resp);

            }
            os.write("00000000000".getBytes());
            System.out.println("client1发送之后");
            bytes = new byte[1024];
            read = inputStream.read(bytes);
            if(read != -1){
                String resp = new String(bytes, 0, read);
                System.out.println(resp);
                if(resp.endsWith("2")){
                    break;
                }

            }
        }
        client.close();



    }

    @Test
    public void client2() throws IOException, InterruptedException {

        OutputStream os = client.getOutputStream();

        InputStream inputStream = client.getInputStream();
        while (true){
            Thread.sleep(2000);
            System.out.println("client2发送之前");
            os.write("1111111111112".getBytes());
            System.out.println("client2发送之后");
            byte[] bytes = new byte[1024];
            int read = inputStream.read(bytes);
            if(read != -1){
                String resp = new String(bytes, 0, read);
                System.out.println(resp);
                if(resp.endsWith("2")){
                    System.out.println("closed...");
                    break;
                }
            }
        }
        client.close();
    }
    @Test
    public void server() throws IOException {
        ServerSocket server = new ServerSocket(6666);
        System.out.println("服务器启动");
        ExecutorService executorService = Executors.newCachedThreadPool();
        while (true){
            final Socket accept = server.accept();
            executorService.execute(() ->{
                handler(accept);
            });
        }
    }
    public static void handler(Socket socket) {

        try {
            System.out.println("线程信息 id =" + Thread.currentThread().getId() + " 名字=" + Thread.currentThread().getName());

            //通过socket 获取输入流
            InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();
            byte[] bytes = new byte[1024];
            //循环的读取客户端发送的数据
            while (true) {

                System.out.println("线程信息 id =" + Thread.currentThread().getId() + " 名字=" + Thread.currentThread().getName());
                int read = inputStream.read(bytes);
                Thread.sleep(3000);

                if (read != -1) {
                    System.out.println(new String(bytes, 0, read
                    )); //输出客户端发送的数据
                    outputStream.write("015002".getBytes());
                } else {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("关闭和client的连接");
            try {
                socket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
